/*
 * @Author: ColdV
 * @Date: 2021-08-04 23:11:36
 * @LastEditTime: 2021-08-06 15:42:10
 * @LastEditors: ColdV
 * @Description: 
 * @FilePath: /chaos_alpha/src/common/thread/ThreadPool.h
 * 
 */
#pragma once

#include <functional>
#include <atomic>
#include <memory>
#include "stdafx.h"
#include "Thread.h"
#include "Mutex.h"
#include "Sem.h"
#include "Condition.h"


namespace chaos
{
	class ThreadPool : public NonCopyable
	{
	public:
		//这个值用来在线程中等待信号的超时时间,不要永久阻塞等待
		//由于在stop的时候发出BroadCast的时候,
		//部分线程可能处m_running判断之后和等待信号之前
		//防止线程池实际已停止,但仍阻塞在等待中
		static const int THREAD_WAIT_TIMEOUT = 10;

		typedef std::function<void()>	ThreadTask;

		explicit ThreadPool(int nThreadNum = -1);

		~ThreadPool();

		int Run();

		int Stop();

		void PushTask(const ThreadTask& task);
		
	private:
		void PoolWorkFunc();

	private:
		std::vector<std::unique_ptr<Thread>> m_threads;
		std::queue<ThreadTask> m_tq;
		uint32 m_threadNum;
		Mutex m_mutex;
		Condition m_cond; 
		std::atomic<bool> m_running;
	};
}	// namespace chaos

